﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using HYSoft.IDAL;
using HYSoft.DBUtility;//Please add references
namespace HYSoft.SQLServerDAL
{
	/// <summary>
	/// 数据访问类:gprsNode
	/// </summary>
	public partial class gprsNode:IgprsNode
	{
		public gprsNode()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("index", "gprsNode"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int index)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from gprsNode");
			strSql.Append(" where [index]=@index");
			SqlParameter[] parameters = {
					new SqlParameter("@index", SqlDbType.Int,4)
			};
			parameters[0].Value = index;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(HYSoft.Model.gprsNode model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into gprsNode(");
			strSql.Append("mac,simno,addr,msgno,areano,adminno,intvalue1,intvalue2,intvalue3,charvalue1,charvalue2,charvalue3,wendu,dianchi,gongdian,dianliang,chuanganqi,jingbao,tux,value0,rate0,value1,rate1,value2,rate2,value3,rate3,value4,rate4,value5,rate5)");
			strSql.Append(" values (");
			strSql.Append("@mac,@simno,@addr,@msgno,@areano,@adminno,@intvalue1,@intvalue2,@intvalue3,@charvalue1,@charvalue2,@charvalue3,@wendu,@dianchi,@gongdian,@dianliang,@chuanganqi,@jingbao,@tux,@value0,@rate0,@value1,@rate1,@value2,@rate2,@value3,@rate3,@value4,@rate4,@value5,@rate5)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@mac", SqlDbType.Int,4),
					new SqlParameter("@simno", SqlDbType.VarChar,50),
					new SqlParameter("@addr", SqlDbType.VarChar,250),
					new SqlParameter("@msgno", SqlDbType.VarChar,50),
					new SqlParameter("@areano", SqlDbType.Int,4),
					new SqlParameter("@adminno", SqlDbType.Int,4),
					new SqlParameter("@intvalue1", SqlDbType.Int,4),
					new SqlParameter("@intvalue2", SqlDbType.Int,4),
					new SqlParameter("@intvalue3", SqlDbType.Int,4),
					new SqlParameter("@charvalue1", SqlDbType.VarChar,250),
					new SqlParameter("@charvalue2", SqlDbType.VarChar,250),
					new SqlParameter("@charvalue3", SqlDbType.VarChar,250),
					new SqlParameter("@wendu", SqlDbType.VarChar,250),
					new SqlParameter("@dianchi", SqlDbType.VarChar,250),
					new SqlParameter("@gongdian", SqlDbType.VarChar,250),
					new SqlParameter("@dianliang", SqlDbType.VarChar,250),
					new SqlParameter("@chuanganqi", SqlDbType.VarChar,250),
					new SqlParameter("@jingbao", SqlDbType.VarChar,250),
					new SqlParameter("@tux", SqlDbType.VarChar,250),
					new SqlParameter("@value0", SqlDbType.Int,4),
					new SqlParameter("@rate0", SqlDbType.Int,4),
					new SqlParameter("@value1", SqlDbType.Int,4),
					new SqlParameter("@rate1", SqlDbType.Int,4),
					new SqlParameter("@value2", SqlDbType.Int,4),
					new SqlParameter("@rate2", SqlDbType.Int,4),
					new SqlParameter("@value3", SqlDbType.Int,4),
					new SqlParameter("@rate3", SqlDbType.Int,4),
					new SqlParameter("@value4", SqlDbType.Int,4),
					new SqlParameter("@rate4", SqlDbType.Int,4),
					new SqlParameter("@value5", SqlDbType.Int,4),
					new SqlParameter("@rate5", SqlDbType.Int,4)};
			parameters[0].Value = model.mac;
			parameters[1].Value = model.simno;
			parameters[2].Value = model.addr;
			parameters[3].Value = model.msgno;
			parameters[4].Value = model.areano;
			parameters[5].Value = model.adminno;
			parameters[6].Value = model.intvalue1;
			parameters[7].Value = model.intvalue2;
			parameters[8].Value = model.intvalue3;
			parameters[9].Value = model.charvalue1;
			parameters[10].Value = model.charvalue2;
			parameters[11].Value = model.charvalue3;
			parameters[12].Value = model.wendu;
			parameters[13].Value = model.dianchi;
			parameters[14].Value = model.gongdian;
			parameters[15].Value = model.dianliang;
			parameters[16].Value = model.chuanganqi;
			parameters[17].Value = model.jingbao;
			parameters[18].Value = model.tux;
			parameters[19].Value = model.value0;
			parameters[20].Value = model.rate0;
			parameters[21].Value = model.value1;
			parameters[22].Value = model.rate1;
			parameters[23].Value = model.value2;
			parameters[24].Value = model.rate2;
			parameters[25].Value = model.value3;
			parameters[26].Value = model.rate3;
			parameters[27].Value = model.value4;
			parameters[28].Value = model.rate4;
			parameters[29].Value = model.value5;
			parameters[30].Value = model.rate5;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(HYSoft.Model.gprsNode model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update gprsNode set ");
			strSql.Append("mac=@mac,");
			strSql.Append("simno=@simno,");
			strSql.Append("addr=@addr,");
			strSql.Append("msgno=@msgno,");
			strSql.Append("areano=@areano,");
			strSql.Append("adminno=@adminno,");
			strSql.Append("intvalue1=@intvalue1,");
			strSql.Append("intvalue2=@intvalue2,");
			strSql.Append("intvalue3=@intvalue3,");
			strSql.Append("charvalue1=@charvalue1,");
			strSql.Append("charvalue2=@charvalue2,");
			strSql.Append("charvalue3=@charvalue3,");
			strSql.Append("wendu=@wendu,");
			strSql.Append("dianchi=@dianchi,");
			strSql.Append("gongdian=@gongdian,");
			strSql.Append("dianliang=@dianliang,");
			strSql.Append("chuanganqi=@chuanganqi,");
			strSql.Append("jingbao=@jingbao,");
			strSql.Append("tux=@tux,");
			strSql.Append("value0=@value0,");
			strSql.Append("rate0=@rate0,");
			strSql.Append("value1=@value1,");
			strSql.Append("rate1=@rate1,");
			strSql.Append("value2=@value2,");
			strSql.Append("rate2=@rate2,");
			strSql.Append("value3=@value3,");
			strSql.Append("rate3=@rate3,");
			strSql.Append("value4=@value4,");
			strSql.Append("rate4=@rate4,");
			strSql.Append("value5=@value5,");
			strSql.Append("rate5=@rate5");
			strSql.Append(" where [index]=@index");
			SqlParameter[] parameters = {
					new SqlParameter("@mac", SqlDbType.Int,4),
					new SqlParameter("@simno", SqlDbType.VarChar,50),
					new SqlParameter("@addr", SqlDbType.VarChar,250),
					new SqlParameter("@msgno", SqlDbType.VarChar,50),
					new SqlParameter("@areano", SqlDbType.Int,4),
					new SqlParameter("@adminno", SqlDbType.Int,4),
					new SqlParameter("@intvalue1", SqlDbType.Int,4),
					new SqlParameter("@intvalue2", SqlDbType.Int,4),
					new SqlParameter("@intvalue3", SqlDbType.Int,4),
					new SqlParameter("@charvalue1", SqlDbType.VarChar,250),
					new SqlParameter("@charvalue2", SqlDbType.VarChar,250),
					new SqlParameter("@charvalue3", SqlDbType.VarChar,250),
					new SqlParameter("@wendu", SqlDbType.VarChar,250),
					new SqlParameter("@dianchi", SqlDbType.VarChar,250),
					new SqlParameter("@gongdian", SqlDbType.VarChar,250),
					new SqlParameter("@dianliang", SqlDbType.VarChar,250),
					new SqlParameter("@chuanganqi", SqlDbType.VarChar,250),
					new SqlParameter("@jingbao", SqlDbType.VarChar,250),
					new SqlParameter("@tux", SqlDbType.VarChar,250),
					new SqlParameter("@value0", SqlDbType.Int,4),
					new SqlParameter("@rate0", SqlDbType.Int,4),
					new SqlParameter("@value1", SqlDbType.Int,4),
					new SqlParameter("@rate1", SqlDbType.Int,4),
					new SqlParameter("@value2", SqlDbType.Int,4),
					new SqlParameter("@rate2", SqlDbType.Int,4),
					new SqlParameter("@value3", SqlDbType.Int,4),
					new SqlParameter("@rate3", SqlDbType.Int,4),
					new SqlParameter("@value4", SqlDbType.Int,4),
					new SqlParameter("@rate4", SqlDbType.Int,4),
					new SqlParameter("@value5", SqlDbType.Int,4),
					new SqlParameter("@rate5", SqlDbType.Int,4),
					new SqlParameter("@index", SqlDbType.Int,4)};
			parameters[0].Value = model.mac;
			parameters[1].Value = model.simno;
			parameters[2].Value = model.addr;
			parameters[3].Value = model.msgno;
			parameters[4].Value = model.areano;
			parameters[5].Value = model.adminno;
			parameters[6].Value = model.intvalue1;
			parameters[7].Value = model.intvalue2;
			parameters[8].Value = model.intvalue3;
			parameters[9].Value = model.charvalue1;
			parameters[10].Value = model.charvalue2;
			parameters[11].Value = model.charvalue3;
			parameters[12].Value = model.wendu;
			parameters[13].Value = model.dianchi;
			parameters[14].Value = model.gongdian;
			parameters[15].Value = model.dianliang;
			parameters[16].Value = model.chuanganqi;
			parameters[17].Value = model.jingbao;
			parameters[18].Value = model.tux;
			parameters[19].Value = model.value0;
			parameters[20].Value = model.rate0;
			parameters[21].Value = model.value1;
			parameters[22].Value = model.rate1;
			parameters[23].Value = model.value2;
			parameters[24].Value = model.rate2;
			parameters[25].Value = model.value3;
			parameters[26].Value = model.rate3;
			parameters[27].Value = model.value4;
			parameters[28].Value = model.rate4;
			parameters[29].Value = model.value5;
			parameters[30].Value = model.rate5;
			parameters[31].Value = model.index;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
        //更新数据，将监测点为areano的换成值为value
        public bool Updateareano(int areano,int value)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update gprsNode set ");
            strSql.Append("areano=@value ");
            strSql.Append(" where areano=@areano");
            SqlParameter[] parameters = {
				    new SqlParameter("@value",SqlDbType.Int,4),
					new SqlParameter("@areano", SqlDbType.Int,4),};
            parameters[0].Value = value;
            parameters[1].Value = areano;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int index)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from gprsNode ");
			strSql.Append(" where [index]=@index");
			SqlParameter[] parameters = {
					new SqlParameter("@index", SqlDbType.Int,4)
			};
			parameters[0].Value = index;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string indexlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from gprsNode ");
			strSql.Append(" where [index] in ("+indexlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public HYSoft.Model.gprsNode GetModel(int index)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 [index],mac,simno,addr,msgno,areano,adminno,intvalue1,intvalue2,intvalue3,charvalue1,charvalue2,charvalue3,wendu,dianchi,gongdian,dianliang,chuanganqi,jingbao,tux,value0,rate0,value1,rate1,value2,rate2,value3,rate3,value4,rate4,value5,rate5 from gprsNode ");
			strSql.Append(" where [index]=@index");
			SqlParameter[] parameters = {
					new SqlParameter("@index", SqlDbType.Int,4)
			};
			parameters[0].Value = index;

			HYSoft.Model.gprsNode model=new HYSoft.Model.gprsNode();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public HYSoft.Model.gprsNode DataRowToModel(DataRow row)
		{
			HYSoft.Model.gprsNode model=new HYSoft.Model.gprsNode();
			if (row != null)
			{
				if(row["index"]!=null && row["index"].ToString()!="")
				{
					model.index=int.Parse(row["index"].ToString());
				}
				if(row["mac"]!=null && row["mac"].ToString()!="")
				{
					model.mac=int.Parse(row["mac"].ToString());
				}
				if(row["simno"]!=null)
				{
					model.simno=row["simno"].ToString();
				}
				if(row["addr"]!=null)
				{
					model.addr=row["addr"].ToString();
				}
				if(row["msgno"]!=null)
				{
					model.msgno=row["msgno"].ToString();
				}
				if(row["areano"]!=null && row["areano"].ToString()!="")
				{
					model.areano=int.Parse(row["areano"].ToString());
				}
				if(row["adminno"]!=null && row["adminno"].ToString()!="")
				{
					model.adminno=int.Parse(row["adminno"].ToString());
				}
				if(row["intvalue1"]!=null && row["intvalue1"].ToString()!="")
				{
					model.intvalue1=int.Parse(row["intvalue1"].ToString());
				}
				if(row["intvalue2"]!=null && row["intvalue2"].ToString()!="")
				{
					model.intvalue2=int.Parse(row["intvalue2"].ToString());
				}
				if(row["intvalue3"]!=null && row["intvalue3"].ToString()!="")
				{
					model.intvalue3=int.Parse(row["intvalue3"].ToString());
				}
				if(row["charvalue1"]!=null)
				{
					model.charvalue1=row["charvalue1"].ToString();
				}
				if(row["charvalue2"]!=null)
				{
					model.charvalue2=row["charvalue2"].ToString();
				}
				if(row["charvalue3"]!=null)
				{
					model.charvalue3=row["charvalue3"].ToString();
				}
				if(row["wendu"]!=null)
				{
					model.wendu=row["wendu"].ToString();
				}
				if(row["dianchi"]!=null)
				{
					model.dianchi=row["dianchi"].ToString();
				}
				if(row["gongdian"]!=null)
				{
					model.gongdian=row["gongdian"].ToString();
				}
				if(row["dianliang"]!=null)
				{
					model.dianliang=row["dianliang"].ToString();
				}
				if(row["chuanganqi"]!=null)
				{
					model.chuanganqi=row["chuanganqi"].ToString();
				}
				if(row["jingbao"]!=null)
				{
					model.jingbao=row["jingbao"].ToString();
				}
				if(row["tux"]!=null)
				{
					model.tux=row["tux"].ToString();
				}
				if(row["value0"]!=null && row["value0"].ToString()!="")
				{
					model.value0=int.Parse(row["value0"].ToString());
				}
				if(row["rate0"]!=null && row["rate0"].ToString()!="")
				{
					model.rate0=int.Parse(row["rate0"].ToString());
				}
				if(row["value1"]!=null && row["value1"].ToString()!="")
				{
					model.value1=int.Parse(row["value1"].ToString());
				}
				if(row["rate1"]!=null && row["rate1"].ToString()!="")
				{
					model.rate1=int.Parse(row["rate1"].ToString());
				}
				if(row["value2"]!=null && row["value2"].ToString()!="")
				{
					model.value2=int.Parse(row["value2"].ToString());
				}
				if(row["rate2"]!=null && row["rate2"].ToString()!="")
				{
					model.rate2=int.Parse(row["rate2"].ToString());
				}
				if(row["value3"]!=null && row["value3"].ToString()!="")
				{
					model.value3=int.Parse(row["value3"].ToString());
				}
				if(row["rate3"]!=null && row["rate3"].ToString()!="")
				{
					model.rate3=int.Parse(row["rate3"].ToString());
				}
				if(row["value4"]!=null && row["value4"].ToString()!="")
				{
					model.value4=int.Parse(row["value4"].ToString());
				}
				if(row["rate4"]!=null && row["rate4"].ToString()!="")
				{
					model.rate4=int.Parse(row["rate4"].ToString());
				}
				if(row["value5"]!=null && row["value5"].ToString()!="")
				{
					model.value5=int.Parse(row["value5"].ToString());
				}
				if(row["rate5"]!=null && row["rate5"].ToString()!="")
				{
					model.rate5=int.Parse(row["rate5"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select [index],mac,simno,addr,msgno,areano,adminno,intvalue1,intvalue2,intvalue3,charvalue1,charvalue2,charvalue3,wendu,dianchi,gongdian,dianliang,chuanganqi,jingbao,tux,value0,rate0,value1,rate1,value2,rate2,value3,rate3,value4,rate4,value5,rate5 ");
			strSql.Append(" FROM gprsNode ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetListadmin(string sql)
        {
            StringBuilder strSql = new StringBuilder();
            if (sql.Trim() != "")
            {
                strSql.Append(sql);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" [index],mac,simno,addr,msgno,areano,adminno,intvalue1,intvalue2,intvalue3,charvalue1,charvalue2,charvalue3,wendu,dianchi,gongdian,dianliang,chuanganqi,jingbao,tux,value0,rate0,value1,rate1,value2,rate2,value3,rate3,value4,rate4,value5,rate5 ");
			strSql.Append(" FROM gprsNode ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM gprsNode ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.index desc");
			}
			strSql.Append(")AS Row, T.*  from gprsNode T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "gprsNode";
			parameters[1].Value = "index";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

